package com.zdp.provider.controller;

import com.zdp.provider.service.CourseService;
import com.zdp.provider.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * 类描述: 教师信息服务
 *
 * @author ZhhengDP
 * @since 2021/8/16
 */
@RestController
@RequestMapping("/teacher_info")
public class TeacherController {

    private RestTemplate restTemplate;

    private TeacherService teacherService;

    private CourseService courseService;

    @Autowired
    public TeacherController(RestTemplate restTemplate, TeacherService teacherService,
                             CourseService courseService) {
        this.restTemplate = restTemplate;
        this.teacherService = teacherService;
        this.courseService = courseService;
    }

    @GetMapping("/teacher")
    public TeacherVo getTeacher(Integer teacherId){
         TeacherVo teacher = teacherService.getTeacherById(teacherId);
         List<CourseDto> courses = courseService.getCourses(teacherId);
         teacher.setCourses(courses);
         return teacher;
    }


    protected List<CourseDto> loadCourses(Integer teacherId){
        // 使用服务名来进行服务的调用
        String url = "http://courseService/course_info/courses";

        List<CourseDto> result = restTemplate.getForObject(url, List.class, teacherId);
        return result;
    }
}
